package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.dto.AppDto;
import org.admin.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/app")
public class AppController {
    @Autowired
    private AppService service;
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<AppDto>addHandle("params", data -> {
                    new AppDto().validate(new String[] {
                            "name", "module", "*current", "size"
                    }).check(data);
                }).addHandle("query", service::query)
                .start()
                .getResult();
    }

    @GetMapping("/fetch")
    public DataResult fetch(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<AppDto>addHandle("params", data -> {
                    new AppDto().validate(new String[] {
                            "*id"
                    }).check(data);
                }).addHandle("fetch", service::fetch)
                .start().getResult();
    }

    // 创建更新
    @PostMapping("/create")
    public DataResult create(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<AppDto>addHandle("params", data -> {
                    new AppDto().validate(new String[] {
                        "id", "*module", "*name", "detail", "picture",
                    }).check(data);
                }).addHandle("create", service::create)
                .start().getResult();
    }

    @PostMapping("/remove")
    public DataResult remove(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<AppDto>addHandle("params", data ->{
                    new AppDto().validate(new String[] {
                            "ids"
                    }).check(data);
                }).addHandle("remove", service::remove)
                .start().getResult();

    }

}
